﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Arch;
using InvoiceChecking.Data;
using InvoiceChecking.Data.SqlClient;
using InvoiceChecking.Entities;
using InvoiceChecking.Settings.BusinessLayer;

public partial class Admin_VoucherPrinting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Refresh_Click(object sender, EventArgs e)
    {
        DataSet jr = DataRepository.Connections["SunConnectionString"].Provider.SunInvoiceProvider.JR(Convert.ToInt32(tb_Period.Text));
        ddl_BatchNo.DataSource = jr.Tables[0];
        ddl_BatchNo.DataValueField = "JRNAL_NO";
        ddl_BatchNo.DataTextField = "JRNAL_NO";
        ddl_BatchNo.DataBind();
        ddl_BatchNo.Items.Insert(0, new ListItem("... Select ..."));
        ddl_BatchNo.Focus();
    }
    protected void ddl_BatchNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBatchGrid();
        BindAllocRefCombo();
        BindInvoiceDetailsGrid();
        ControlPrintCredit();
    }

    private void ControlPrintCredit()
    {
        //ServiceFacade.SettingsHelper.CreditCardAccountCode = "20101MSB00";
        string whereClause = "JRNAL_NO = " + ddl_BatchNo.SelectedValue;
        int count;
        VList<SunInvoice> journal =
            DataRepository.Connections["SunConnectionString"].Provider.SunInvoiceProvider.GetPaged(whereClause, "JRNAL_LINE", 0, 200, out count);
        for (int i = 0; i < count; i++)
        {
            if (journal[i].AllocRef == Int32.Parse(ddl_AllocRef.SelectedValue))
            {
                if (journal[i + 1].AccntCode.Trim() == ServiceFacade.SettingsHelper.CreditCardAccountCode)
                {
                    ddl_Prefix.Items.Add("CCA");
                    ddl_Prefix.SelectedValue = "CCA";
                    ddl_Prefix.Enabled = false;
                    tr_CreditCard.Style.Remove("display");
                }
                else
                {
                    if (ddl_Prefix.Items.FindByText("CCA") != null)
                        ddl_Prefix.Items.Remove("CCA");
                    ddl_Prefix.SelectedValue = "04A";
                    ddl_Prefix.Enabled = true;
                    tr_CreditCard.Style.Add("display", "none");
                    return;
                }
            }
        }
    }

    private void BindAllocRefCombo()
    {
        DataSet allocRef = DataRepository.SunInvoiceProvider.Journal(Convert.ToInt32(ddl_BatchNo.SelectedValue));
        ddl_AllocRef.DataSource = allocRef;
        ddl_AllocRef.DataValueField = "ALLOC_REF";
        ddl_AllocRef.DataTextField = "DESCRIPTN";
        ddl_AllocRef.DataBind();
    }

    private void BindInvoiceDetailsGrid()
    {
        SunReference allocRef = DataRepository.SunReferenceProvider.GetByAllocRef(Convert.ToInt32(ddl_AllocRef.SelectedValue));

        tb_Description.Text = allocRef != null ? allocRef.Description : "";
        if (cb_InvoiceDetails.Checked)
        {
            int count;
            string whereClause = "ALLOC_REF =" + ddl_AllocRef.SelectedValue;
            VList<SunInvoice> allInvoiceDetails =
                DataRepository.Connections["SunConnectionString"].Provider.SunInvoiceProvider.GetPaged(whereClause, "",
                                                                                                       0,
                                                                                                       200, out count);
            grv_InvoiceDetails.RecordsCount = count;
            grv_InvoiceDetails.DataSource = allInvoiceDetails;
            grv_InvoiceDetails.DataBind();
        }
    }

    private void BindBatchGrid()
    {
        int count = 0;
        string whereClause = "JRNAL_NO = " + ddl_BatchNo.SelectedValue;
        VList<SunInvoice> journal =
            DataRepository.Connections["SunConnectionString"].Provider.SunInvoiceProvider.GetPaged(whereClause, "JRNAL_LINE", 0, 200, out count);
        grv_BatchList.DataSource = journal;
        grv_BatchList.RecordsCount = count;
        grv_BatchList.DataBind();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
        if (httpCookie != null)
        {
            SqlNetTiersProvider provider = GlobalUtilities.CreateSunProvider(Server.MapPath("~") + "/Config.xml",
                                                                          httpCookie.Value);
            if (provider != null)
                DataRepository.LoadProvider(provider, true);
        }
    }

    protected void grv_BatchList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_BatchList.PageIndex = e.NewPageIndex;
        BindBatchGrid();
    }

    protected void grv_InvoiceDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_InvoiceDetails.PageIndex = e.NewPageIndex;
        BindInvoiceDetailsGrid();
    }

    protected void btn_Print_Click(object sender, EventArgs e)
    {
        if (ddl_AllocRef.SelectedItem.Text.Trim().EndsWith("-"))
        {
            //DataRepository.SunReferenceProvider.GetMax();
            string authUserName = Context.User.Identity.Name.Split('\\')[1];
            DataRepository.SunReferenceProvider.CreateSunReference(Convert.ToInt32(ddl_AllocRef.SelectedValue), ddl_Prefix.SelectedValue, authUserName);
            //BindAllocRefCombo();
        }
        SunReference allocRef = DataRepository.SunReferenceProvider.GetByAllocRef(Convert.ToInt32(ddl_AllocRef.SelectedValue));
        allocRef.Description = tb_Description.Text;
        string redirectUrl;
        if (string.Equals(ddl_Prefix.SelectedValue, "CCA"))
        {
            allocRef.CreditCardId = Int32.Parse(ddl_CreditCard.SelectedValue);
            redirectUrl = string.Format("ReportCreditCardPayment.aspx?BatchNo={0}&AllocRef={1}&CreditCardId={2}",
                                           ddl_BatchNo.SelectedValue, ddl_AllocRef.SelectedValue, ddl_CreditCard.SelectedValue);
        }
        else
        {
            redirectUrl = string.Format("ReportExportPayment.aspx?BatchNo={0}&AllocRef={1}",
                                           ddl_BatchNo.SelectedValue, ddl_AllocRef.SelectedValue);
        }
        DataRepository.SunReferenceProvider.Update(allocRef);
        Response.Redirect(redirectUrl);
    }
    protected void ddl_AllocRef_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindInvoiceDetailsGrid();
        ControlPrintCredit();
    }
    protected void btn_RefreshAllocRef_Click(object sender, EventArgs e)
    {
        BindAllocRefCombo();
    }
    protected void grv_BatchList_ExcelBeforeFormat(object sender, InvoiceChecking.Web.UI.ExcelFormatEventArgs e)
    {
        BindBatchGrid();
    }
    protected void grv_InvoiceDetails_ExcelBeforeFormat(object sender, InvoiceChecking.Web.UI.ExcelFormatEventArgs e)
    {
        BindInvoiceDetailsGrid();
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (ddl_AllocRef.SelectedItem.Text.Trim().EndsWith("-"))
        {
            //DataRepository.SunReferenceProvider.GetMax();
            string authUserName = Context.User.Identity.Name.Split('\\')[1];
            DataRepository.SunReferenceProvider.CreateSunReference(Convert.ToInt32(ddl_AllocRef.SelectedValue), ddl_Prefix.SelectedValue, authUserName);
            //BindAllocRefCombo();
        }
        SunReference allocRef = DataRepository.SunReferenceProvider.GetByAllocRef(Convert.ToInt32(ddl_AllocRef.SelectedValue));
        allocRef.Description = tb_Description.Text;
        if (string.Equals(ddl_Prefix.SelectedValue, "CCA"))
            allocRef.CreditCardId = Int32.Parse(ddl_CreditCard.SelectedValue);
        DataRepository.SunReferenceProvider.Update(allocRef);
        btn_Refresh_Click(null, null);
    }
}
